package cn.hznc.controller.schedule;

import cn.hznc.controller.service.inventory.InventoryLocationService;
import cn.hznc.domain.entity.NameNumberEntity;
import cn.hznc.domain.request.common.QueryCondition;
import cn.hznc.domain.request.device.EquipmentOnlineDataReq;
import cn.hznc.domain.request.inventory.OutBoundAssemblyReq;
import cn.hznc.domain.request.process.EmployeeTasksReq;
import cn.hznc.domain.request.schedule.*;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.exception.ToolLifeCycleException;
import cn.hznc.exception.ToolManageException;
import cn.hznc.exception.ToolScheduleException;
import cn.hznc.controller.service.schedule.ToolScheduleService;
import cn.hznc.repository.MachineToolTaskRespository;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author：Administrator
 * @Date：2023/4/21 15:49
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/toolSchedule")
@Slf4j
public class ToolScheduleController {

    @Autowired
    private ToolScheduleService toolScheduleService;


    /**
     * 刀具需求预测（工单）
     *
     * @param toolDemandPredictionQeq
     * @return
     */
    @RequestMapping(value = "/getToolDemandPredictionList", method = RequestMethod.POST)
    public ResponseBody getToolDemandPredictionList(@RequestBody ToolDemandPredictionReq toolDemandPredictionQeq) {
        try {
            return ResponseBody.success(toolScheduleService.getToolDemandPredictionList(toolDemandPredictionQeq));
        } catch (ToolScheduleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 刀具需求预测缺失刀具列表（工单）
     *
     * @return
     */
    @RequestMapping(value = "/getUnmetToolList", method = RequestMethod.POST)
    public ResponseBody getUnmetToolList(@RequestBody UnmetToolReq unmetToolReq) {
        return ResponseBody.success(toolScheduleService.getUnmetToolList(unmetToolReq));
    }

    /**
     * 导出刀具需求预测缺失刀具列表（工单）
     *
     * @return
     */
    @RequestMapping(value = "/exportUnmetToolList", method = RequestMethod.POST)
    public void exportUnmetToolList(@RequestBody ExportUnmetToolList exportUnmetToolList , HttpServletResponse response) {
        toolScheduleService.exportUnmetToolList(exportUnmetToolList , response);
    }

    /**
     * 刀具需求预测（人员）
     *
     * @param empToolDemandPredictionReq
     * @return
     */
    @RequestMapping(value = "/getEmpToolDemandPredictionList", method = RequestMethod.POST)
    public ResponseBody getEmpToolDemandPredictionList(@RequestBody EmpToolDemandPredictionReq empToolDemandPredictionReq) {
        try {
            return ResponseBody.success(toolScheduleService.getEmpToolDemandPredictionList(empToolDemandPredictionReq));
        } catch (ToolScheduleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 刀具需求预测子表（人员）
     *
     * @return
     */
    @RequestMapping(value = "/getEmpToolDemandPredictionChild", method = RequestMethod.GET)
    public ResponseBody getEmpToolDemandPredictionChild(@RequestParam("employeeId") Integer employeeId) {
        return ResponseBody.success(toolScheduleService.getEmpToolDemandPredictionChild(employeeId));
    }

    /**
     * 刀具需求预测（机床）
     *
     * @param macToolDemandPredictionReq
     * @return
     */
    @RequestMapping(value = "/getMacToolDemandPredictionList", method = RequestMethod.POST)
    public ResponseBody getMacToolDemandPredictionList(@RequestBody MacToolDemandPredictionReq macToolDemandPredictionReq) {
        try {
            return ResponseBody.success(toolScheduleService.getMacToolDemandPredictionList(macToolDemandPredictionReq));
        } catch (ToolScheduleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 直接取料
     *
     * @param qryDirectRetrievalReq
     * @return
     */
    @RequestMapping(value = "/getDirectRetrievalList", method = RequestMethod.POST)
    public ResponseBody getDirectRetrievalList(@RequestBody QryDirectRetrievalReq qryDirectRetrievalReq) {
        return ResponseBody.success(toolScheduleService.getDirectRetrievalList(qryDirectRetrievalReq));
    }

    /**
     * 直接取料取刀
     *
     * @return
     */
    @RequestMapping(value = "/directRetrieval", method = RequestMethod.POST)
    public ResponseBody directRetrieval(@RequestBody DirectRetrievalReq directRetrievalReq) {
        try {
            return ResponseBody.success(toolScheduleService.directRetrieval(directRetrievalReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        } catch (ToolManageException e){
            return ResponseBody.error(e.getCode() , e.getMsg());
        }
    }

    /**
     * 人员取料
     *
     * @return
     */
    @RequestMapping(value = "/getEmployeeRetrievalIndex", method = RequestMethod.POST)
    public ResponseBody getEmployeeRetrievalIndex(@RequestBody EmployeeRetrievalIndexReq employeeRetrievalIndexReq) {
        return ResponseBody.success(toolScheduleService.getEmployeeRetrievalIndex(employeeRetrievalIndexReq));
    }

    /**
     * 获取人员任务列表
     *
     * @return
     */
    @RequestMapping(value = "/getEmployeeTaskList", method = RequestMethod.POST)
    public ResponseBody getEmployeeTaskList(@RequestBody EmployeeTasksReq employeeTasksReq) {
        return ResponseBody.success(toolScheduleService.getEmployeeTaskList(employeeTasksReq));
    }

    /**
     * 根据taskId获取orderIds
     *
     * @return
     */
    @RequestMapping(value = "/getOrderIdsByTaskIds", method = RequestMethod.GET)
    public ResponseBody<List<Integer>> getOrderIdsByTaskIds(@RequestParam("taskIds") String taskIds) {
        try {
            return ResponseBody.success(toolScheduleService.getOrderIdsByTaskIds(taskIds));
        } catch (ToolManageException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 人员取料任务详情页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/getEmployeeRetrievalDetail", method = RequestMethod.GET)
    public ResponseBody getEmployeeRetrievalDetail(@RequestParam("id") Integer id,
                                                   @RequestParam("pageSize") Integer pageSize,
                                                   @RequestParam("pageNum") Integer pageNum) {
        return ResponseBody.success(toolScheduleService.getEmployeeRetrievalDetail(id , pageNum , pageSize));
    }

    /**
     * 人员取料任务详情页面
     * @param getEmployeeTaskDetailReq
     * @return
     */
    @RequestMapping(value = "/getEmployeeTaskDetail", method = RequestMethod.POST)
    public ResponseBody getEmployeeTaskDetail(@RequestBody GetEmployeeTaskDetailReq getEmployeeTaskDetailReq) {
        return ResponseBody.success(toolScheduleService.getEmployeeTaskDetail(getEmployeeTaskDetailReq));
    }

    /**
     * 人员取料任务取料清单
     * @param taskDemandPredictionReq
     * @return
     */
    @RequestMapping(value = "/getTaskRetrievalDetail", method = RequestMethod.GET)
    public ResponseBody getTaskRetrievalDetail(@RequestBody TaskDemandPredictionReq taskDemandPredictionReq) {
        return ResponseBody.success(toolScheduleService.getTaskRetrievalDetail(taskDemandPredictionReq));
    }

    /**
     * 人员取料清单页面总成取刀分级仓库库存
     * @param toolNo
     * @return
     */
    @RequestMapping(value = "/getWarehouseInventoryByToolNo", method = RequestMethod.GET)
    public ResponseBody getWarehouseInventoryByToolNo(@RequestParam("toolNo") String toolNo) {
        return ResponseBody.success(toolScheduleService.getWarehouseInventoryByToolNo(toolNo));
    }

    /**
     * 人员取料清单页面总成取刀库位库
     * @return
     */
    @RequestMapping(value = "/getLocationInventory", method = RequestMethod.POST)
    public ResponseBody getLocationInventory(@RequestParam("type") String type) {
        return ResponseBody.success(toolScheduleService.getLocationInventory(type));
    }

    /**
     * 人员取料清单页面总成取刀出口
     * @return
     */
    @RequestMapping(value = "/empOutboundAssemblyTool", method = RequestMethod.POST)
    public ResponseBody empOutboundAssemblyTool(@RequestBody EmpOutboundAssemblyToolReq empOutboundAssemblyToolReq) {
        return toolScheduleService.empOutboundAssemblyTool(empOutboundAssemblyToolReq);
    }

    @RequestMapping(value = "/empOutboundAssemblyToolList", method = RequestMethod.POST)
    public ResponseBody empOutboundAssemblyToolList(@RequestBody EmpOutboundAssemblyToolReq empOutboundAssemblyToolReq) {
        return ResponseBody.success(toolScheduleService.empOutboundAssemblyToolList(empOutboundAssemblyToolReq));
    }

    @RequestMapping(value = "/empInboundAssemblyTool", method = RequestMethod.POST)
    public ResponseBody empInboundAssemblyTool(@RequestBody EmpInboundAssemblyToolReq empInboundAssemblyToolReq) {
        return toolScheduleService.empInboundAssemblyTool(empInboundAssemblyToolReq);
    }

    @RequestMapping(value = "/equipmentOnlineData", method = RequestMethod.POST)
    public ResponseBody equipmentOnlineData(@RequestBody EquipmentOnlineDataReq equipmentOnlineDataReq) {
        return ResponseBody.success(toolScheduleService.equipmentOnlineData(equipmentOnlineDataReq));
    }

    @RequestMapping(value = "/equipmentOnlineDataReload", method = RequestMethod.POST)
    public ResponseBody equipmentOnlineDataReload(@RequestBody EquipmentOnlineDataReq equipmentOnlineDataReq) {
        return ResponseBody.success(toolScheduleService.equipmentOnlineDataReload(equipmentOnlineDataReq));
    }

    @RequestMapping(value = "/equipmentOnlineProgram", method = RequestMethod.GET)
    public ResponseBody equipmentOnlineProgram(@RequestParam("warehouseId") Integer warehouseId) {
        try {
            return ResponseBody.success(toolScheduleService.equipmentOnlineProgram(warehouseId));
        } catch (Exception e) {
            return ResponseBody.error("100001" , "程序采集有误");
        }
    }

    /**
     *获取物料最大用量
     * @return
     */
    @RequestMapping(value = "/queryMaximumMaterialUsage", method = RequestMethod.GET)
    public ResponseBody<List<NameNumberEntity>> queryMaximumMaterialUsage(){
        return ResponseBody.success(toolScheduleService.queryMaximumMaterialUsage());
    }

    /**
     * 取料类别汇总
     * @return
     */
    @RequestMapping(value = "/queryReclaimingCategory", method = RequestMethod.GET)
    public ResponseBody<List<NameNumberEntity>> queryReclaimingCategory(){
        return ResponseBody.success(toolScheduleService.queryReclaimingCategory());
    }

    @RequestMapping(value = "/queryDirectRetrievalList", method = RequestMethod.POST)
    public ResponseBody queryDirectRetrievalList(@RequestBody QryDirectRetrievalReq qryDirectRetrievalReq) {
        return ResponseBody.success(toolScheduleService.queryDirectRetrievalList(qryDirectRetrievalReq));
    }

    @RequestMapping(value = "/queryDirectRetrievalIndex", method = RequestMethod.POST)
    public ResponseBody queryDirectRetrievalIndex(@RequestBody QryDirectRetrievalReq qryDirectRetrievalReq) {
        return ResponseBody.success(toolScheduleService.queryDirectRetrievalIndex(qryDirectRetrievalReq));
    }

    @RequestMapping(value = "/queryAssemblyRetrieval", method = RequestMethod.POST)
    public ResponseBody queryAssemblyRetrieval(@RequestBody QryDirectRetrievalReq qryDirectRetrievalReq) {
        return ResponseBody.success(toolScheduleService.queryAssemblyRetrieval(qryDirectRetrievalReq));
    }
}
